package com.qf.service.impl;/*
 *徐韬
 *2021/10/29
 *20:20
 */

import com.github.pagehelper.PageInfo;
import com.qf.cache.paramsCache;
import com.qf.cache.tsmsTemplateCache;
import com.qf.constons.resultCode;
import com.qf.events.UpdateSmsEvents;
import com.qf.exceptions.addDataException;
import com.qf.exceptions.updateException;
import com.qf.mapper.smstempMapper;
import com.qf.pojo.*;
import com.qf.service.smsTemplateService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class smsTemplateServiceimpl implements smsTemplateService {

    private tsmsTemplateCache tsmsTemplateCache;
    private paramsCache paramsCache;
@Autowired
    public void setParamsCache(com.qf.cache.paramsCache paramsCache) {
        this.paramsCache = paramsCache;
    }

    @Autowired
    public void setTsmsTemplateCache(com.qf.cache.tsmsTemplateCache tsmsTemplateCache) {
        this.tsmsTemplateCache = tsmsTemplateCache;
    }

    private ApplicationContext context;
@Autowired
    public void setContext(ApplicationContext context) {
        this.context = context;
    }

    private smstempMapper smstempMapper;
@Autowired
    public void setSmstempMapper(com.qf.mapper.smstempMapper smstempMapper) {
        this.smstempMapper = smstempMapper;
    }

    @Override
    public void addsms(TSmsTemplate tSmsTemplate) {
        if(tSmsTemplate.isNull(checkType.ADD)){
            throw new addDataException("传输的参数为空", resultCode.DATA_ISNULL);
        }
        smstempMapper.addsms(tSmsTemplate);
        context.publishEvent(new UpdateSmsEvents());
    }

    @Override
    public PageInfo<TSmsTemplate> showAll(Integer pageNum, Integer pageSize) {
        List<TSmsTemplate> templates1 = tsmsTemplateCache.getfinlAll();
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        MyAuthUser myAuthUser = (MyAuthUser) principal;
        List<TSmsTemplate> templates = templates1.stream().filter(sms -> {
            return myAuthUser.getType() == 1 || sms.getCreater() == myAuthUser.getId() || sms.getOwntype() == 1;
        }).collect(Collectors.toList());
        List<TSmsTemplate> templateList = templates.stream().skip((pageNum - 1) * pageSize).limit(pageSize).collect(Collectors.toList());
        PageInfo<TSmsTemplate> pageInfo = new PageInfo<>(templateList);
        pageInfo.setTotal(templates.size());
        return pageInfo;
    }

    @Override
    public void update(TSmsTemplate tSmsTemplate) {
        if(tSmsTemplate.isNull(checkType.UPDATE)){
            throw new updateException("参数为空",resultCode.DATA_ISNULL);
        }
        TSmsTemplate tSmsTemplate1 = smstempMapper.selectById(tSmsTemplate.getId());
        if(tSmsTemplate.getOwntype() != 1 && tSmsTemplate.getCreater() != tSmsTemplate1.getCreater() ){
            throw new updateException("您无权修改该条数据",resultCode.NO_ROOT);
        }
        smstempMapper.update(tSmsTemplate);
        context.publishEvent(new UpdateSmsEvents());
    }

    @Override
    public void deleteByis(List<Integer> ids) {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        ServletRequestAttributes servletRequestAttributes =(ServletRequestAttributes) requestAttributes;
        TAdminUser  user=(TAdminUser) servletRequestAttributes.getRequest().getSession().getAttribute("user");
        if(user.getType() ==1){
            smstempMapper.deleteByids(ids);
            return;
        }
        smstempMapper.deleteByidsandcreater(ids,user.getId());
        context.publishEvent(new UpdateSmsEvents());

    }

    @Override
    public List<TSmsTemplate> showAllByParams(TSmsTemplate tSmsTemplate) {
        System.out.println(tsmsTemplateCache);
        Class<? extends TSmsTemplate> smsTemplateClass = tSmsTemplate.getClass();
        TSmsTemplate instance = null;//创建一个新的是对象
        try {
            instance = smsTemplateClass.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        List<String> params = new ArrayList<>();
        Params sms = paramsCache.getById("sms");
        String param1 = sms.getParam1();
        String param2 = sms.getParam2();
        String param3 = sms.getParam3();
        if(param1 != null){
            params.add(param1);
        }
        if(param1 != null){
            params.add(param2);
        }
        if(param1 != null){
            params.add(param3);
        }

        TSmsTemplate instance1 = instance;
        params.forEach(name ->{
            try {
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(name,smsTemplateClass);
                Method writeMethod = propertyDescriptor.getWriteMethod();
                Method readMethod = propertyDescriptor.getReadMethod();
                writeMethod.invoke(instance1,readMethod.invoke(tSmsTemplate));
            } catch (IntrospectionException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }


        });
        System.out.println(instance1);
        List<TSmsTemplate> templates = smstempMapper.showAllByparams(instance1);
        return templates;

    }
}
